package com.sunny.l66;

/**
 * @Author zsunny
 * @Date 2018/9/19 17:29
 * @Mail zsunny@yeah.net
 */
public class Solution {

    public static void main(String[] args) {

        Solution solution = new Solution();

        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(1);
        ListNode node3 = new ListNode(2);
        ListNode node4 = new ListNode(2);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
//        solution.print(node1);

        ListNode head = solution.deleteDuplicates(node1);

//        solution.print(head);

    }

    public void print(ListNode head){
        ListNode node = head;
        while (null != node){
            System.out.print(node.val+" ");
            node = node.next;
        }
        System.out.println();
    }

    public ListNode deleteDuplicates(ListNode head) {
        if(null == head || null == head.next)
            return head;
        ListNode newHead = new ListNode(0);
        newHead.next = head;
        head = newHead;
        ListNode prev = head;
        ListNode cur = prev.next;
        boolean flag = false;
        while (null != cur && null != cur.next){
            if (cur.val == cur.next.val){
                flag = true;
            }else{
                if(flag) {
                    prev.next = cur.next;
                    flag = false;
                }else{
                    prev = cur;
                }
            }
            cur = cur.next;
        }
        if(flag){
            prev.next = null;
        }
        return head.next;
    }
}

class ListNode {
    int val;
    ListNode next;
    ListNode(int x) {
     val = x;
     next = null;
    }
}
